package com.cy.网络编程.tcp.test1;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

/**
 * 服务器
 */
public class Server {
    public static void main(String[] args) throws Exception {
        //1. 创建服务器对象
        ServerSocket server = new ServerSocket(9999);
        //2. 在这等待客户端连接
        System.out.println("等待客户端连接......");
        Socket s1 = server.accept();//阻塞
        System.out.println(s1);//s1是连接的服务器对象
        System.out.println("客户端连接成功！！！");

        while (true) {
            Scanner sc = new Scanner(System.in);
            //服务器接受消息
            InputStream is = s1.getInputStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(is));
            String s = br.readLine();
            System.out.println("从客户端发送过来的消息是：" + s);


            //服务器发送消息
            OutputStream os = s1.getOutputStream();
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os));
            bw.write(sc.nextLine());
            bw.newLine();
            bw.flush();
        }
    }
}
